﻿namespace Resharper.Abstractions.CodeDom.TypeMembers.Fields.Constants
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.CSharp.Tree;
  using Resharper.Abstractions.Actions;

  /// <summary>Defines the <see cref="ConstantExtensions"/> class.</summary>
  public static class ConstantExtensions
  {
    #region Public Methods

    /// <summary>Creates the constant.</summary>
    /// <param name="factory">The factory.</param>
    /// <param name="code">The code.</param>
    /// <returns>Returns the constant.</returns>
    public static Constant CreateConstant([NotNull] this Factory factory, [NotNull] string code)
    {
      if (factory == null)
      {
        throw new ArgumentNullException("factory");
      }

      if (code == null)
      {
        throw new ArgumentNullException("code");
      }

      var declaration = factory.InternalFactory.CreateTypeMemberDeclaration(code) as IConstantDeclaration;

      return declaration != null ? new Constant(declaration) : null;
    }

    #endregion
  }
}